package com.jzo2o.market.handler;

import com.jzo2o.market.service.ICouponService;
import com.jzo2o.redis.handler.SyncProcessHandler;
import com.jzo2o.redis.model.SyncMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

import static com.jzo2o.market.constants.RedisConstants.RedisKey.COUPON_SEIZE_SYNC_QUEUE_NAME;

/**
 * 同步数据处理器实现类
 *
 * @author Zhy
 * @version 1.0
 * @date 2025-04-09 16:53
 */
@Component(COUPON_SEIZE_SYNC_QUEUE_NAME)
@Slf4j
public class SeizeCouponSyncProcessHandler implements SyncProcessHandler<Object> {

	@Resource
	private ICouponService couponService;

	@Override
	public void batchProcess(List<SyncMessage<Object>> multiData) {
		log.info("暂不支持批量处理");
	}

	@Override
	public void singleProcess(SyncMessage<Object> singleData) {
		// key = 用户id_活动id
		String key = singleData.getKey();
		log.info("处理单条数据，key={}:{}", key, singleData.getValue());

		// 1. 解析出用户id和活动id
		String[] ids = key.replace("\"", "").split("_");
		Long userId = Long.valueOf(ids[0]);
		Long activityId = Long.valueOf(ids[1]);

		// 2. 向优惠表中增加记录，并更新活动库存
		couponService.addCouponReceiveRecord(userId, activityId);
	}

}
